package com.esteel.settlement.dao.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;

import com.esteel.settlement.dao.ITbSettPayRepository;
import com.esteel.settlement.entity.TbSettFundChg;
import com.esteel.settlement.vo.PayQueryVo;
import com.esteel.settlement.vo.TbSettPayVo;

/**
 * 交易列表查询实现类
 * 
 * @author zhangxiuzhi
 *
 */
public class TbSettPayRepositoryImpl implements ITbSettPayRepository {

	@Autowired
	private PageQuery pageQuery;

	@Override
	public Page<TbSettPayVo> payQuery(PayQueryVo pay) {
		StringBuilder sb = new StringBuilder(
				"select co.cust_name as outCustName,ci.cust_name as inCustName,p.* from tb_sett_pay p,tb_sett_account co,tb_sett_account ci where p.out_cust_acct_id = co.cust_acct_id and p.in_cust_acct_id = ci.cust_acct_id");

		List<Object> args = new ArrayList<Object>();

		// 付款方会员号
		if (!StringUtils.isEmpty(pay.getOutThirdCustAcctId())) {
			sb.append(" and p.out_third_cust_id = ? ");
			args.add(pay.getOutThirdCustAcctId());
		}

		// 收款方会员号
		if (!StringUtils.isEmpty(pay.getInThirdCustAcctId())) {
			sb.append(" and p.in_third_cust_id = ? ");
			args.add(pay.getInThirdCustAcctId());
		}

		// 订单号
		if (!StringUtils.isEmpty(pay.getOrderNo())) {
			sb.append(" and p.third_ht_id = ?");
			args.add(pay.getOrderNo());
		}

		// 支付指令号
		if (!StringUtils.isEmpty(pay.getPaySerialNo())) {
			sb.append(" and p.pay_serial_no = ?");
			args.add(pay.getPaySerialNo());
		}

		// 开始日期
		if (pay.getBegin() != null) {
			sb.append(" and p.tran_date > ?");
			args.add(pay.getBegin());
		}

		// 结束日期
		if (pay.getEnd() != null) {
			sb.append(" and p.tran_date < ?");
			Date end = pay.getEnd();
			Calendar cal = Calendar.getInstance();
			cal.setTime(end);
			cal.add(Calendar.DATE, 1);
			args.add(cal.getTime());
		}

		sb.append(" order by p.pay_id desc");

		return pageQuery.query(sb.toString(), args.toArray(), pay, TbSettPayVo.class);
	}

}
